package com.zhaowb.springcloud.common.rest;

import com.zhaowb.springcloud.common.biz.BaseBiz;
import com.zhaowb.springcloud.common.msg.ObjectRestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IDEA
 *
 * @author zhaowb
 * @date 2018/10/30 21:32
 */
public class BaseController<Biz extends BaseBiz, Entity> {

    private final Logger log = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    private HttpServletRequest request;

    @Autowired
    protected Biz baseBiz;

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<Entity> add(Entity entity) {
        baseBiz.insertSelective(entity);
        return new ObjectRestResponse<Entity>().rel(true);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<Entity> get(@PathVariable() int id) {
        return new ObjectRestResponse<>().result(baseBiz.selectById(id));
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse<Entity> update(Entity entity) {
        baseBiz.updateSelectiveById(entity);
        return new ObjectRestResponse<Entity>().rel(true);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse<Entity> remove(@PathVariable int id) {
        baseBiz.deleteById(id);
        return new ObjectRestResponse<Entity>().rel(true);
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public List<Entity> list() {
        return baseBiz.selectListAll();
    }

    public String getCurrentUserName() {
        String authorization = request.getHeader("Authorization");
        log.info("authorization = {}",authorization);
        return new String(Base64Utils.decodeFromString(authorization));
    }
}
